package task

import (
	"errors"
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"minefieldplatform-go/app/service/task"
	"minefieldplatform-go/library"
	"strings"
)

func GpuMonitorAll(r *ghttp.Request) {
	ids := r.GetFormArray("ids")
	ip := strings.Split(ids[0], ",")
	command := "ps -ef |grep plot|wc -l"
	for _, i := range ip {
		go func(i, command string) {
			res, err := library.RunSsh(i, command)
			if err != nil {
				fmt.Println(errors.New("这里出错了"))
			} else if res == "1" {
				var data = g.Map{"node_status": "运行中"}
				task.GpuMonitorUpdate(data, data["ip"])
			} else {
				var data = g.Map{"node_status": "故障中"}
				task.GpuMonitorUpdate(data, data["ip"])
			}
		}(i, command)
	}
}

func GpuMonitor(r *ghttp.Request) {
	ips := r.Get("ip")
	ip := gconv.String(ips)
	command := "ps -ef |grep plot|wc -l"
	res, err := library.RunSsh(ip, command)
	if err != nil {
		fmt.Println(errors.New("这里出错了"))
	} else if res == "1" {
		var data = g.Map{"node_status": "运行中"}
		task.GpuMonitorUpdate(data, data["ip"])
	} else {
		var data = g.Map{"node_status": "故障中"}
		task.GpuMonitorUpdate(data, data["ip"])
	}
}
